package com.project.config;

import com.project.Main;
import com.project.util.LocalDateChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述)webMVC配置类
 * @Date(创建时间)
 */
@Configuration
@ComponentScan(basePackageClasses = Main.class)
@EnableWebMvc //提供springMVC支持
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    /**
     * 类型转换器
     */
    @PostConstruct
    public void addConversionConfig() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService converService = (GenericConversionService) initializer.getConversionService();
            converService.addConverter(new LocalDateChange());
        }
    }

    /**
     * 设置编码集,只对响应信息有效
     *
     * @param converters
     */
    @Override
    public void extendMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringHttpMessageConverter =
                (StringHttpMessageConverter) converters.get(1);
        stringHttpMessageConverter.setDefaultCharset(
                Charset.forName("utf-8"));
    }

    /**
     * 设置静态页面路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(
            ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/html/**").
                addResourceLocations("classpath:/static/html/");
    }
//
//    /**
//     * 设置Thymeleaf编码集和引擎页面的路径
//     *
//     * @return
//     */
//    @Bean
//    public SpringResourceTemplateResolver templateResolver() {
//        SpringResourceTemplateResolver templateResolver =
//                new SpringResourceTemplateResolver();
//        templateResolver.setCharacterEncoding("utf-8");//设置编码集
//        //设置模版引擎页面的路径
//        templateResolver.setPrefix("classpath:/static/");
//        templateResolver.setCacheable(false);
//        return templateResolver;
//    }
//
//    /**
//     * @return
//     */
//    @Bean
//    public SpringTemplateEngine templateEngine() {
//        SpringTemplateEngine templateEngine
//                = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver());
//        return templateEngine;
//    }
//
//    /**
//     * 设置Thymeleaf，告知什么样的页面需要翻译
//     *
//     * @return
//     */
//    @Bean
//    public ThymeleafViewResolver viewResolver() {
//        ThymeleafViewResolver viewResolver =
//                new ThymeleafViewResolver();
//        viewResolver.setCharacterEncoding("utf-8");
//        viewResolver.setTemplateEngine(templateEngine());
//        viewResolver.setOrder(1);
//        //定义html和xhtml后缀的文件被thymeleaf引擎翻译
//        viewResolver.setViewNames(
//                new String[]{"*.html", "*.xhtml"});
//        return viewResolver;
//    }
//
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        //设置上传文件大小限制
        multipartResolver.setMaxUploadSize(10240000);
        return multipartResolver;
    }
}
